package be.groept.ace.enquete.servlets;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import be.groept.ace.enquete.model.beans.Question;
import be.groept.ace.enquete.model.beans.Survey;

/**
 * Servlet implementation class QuestionServlet
 */
@WebServlet("/user/enquete/question/*")
public class QuestionServlet extends UserServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(QuestionServlet.class);
	@Override
	protected int handleGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("GET");
		String questionIdString = request.getParameter("id");
		if(questionIdString == null) {
			questionIdString = (String)request.getAttribute("id");
		}
		try {
			final int questionId = Integer.parseInt(questionIdString);
			HttpSession session = request.getSession(false);
			Survey survey = (Survey) session.getAttribute("survey");
			Question currentQuestion = null;
			Question previousQuestion = null;
			Question nextQuestion = null;
			int nrQuestion = 0;
			for (Iterator<Question> iterator = survey.getQuestionSet().iterator(); iterator.hasNext();) {
				Question q = iterator.next();
				if (q.getId() == questionId) {
					currentQuestion = q;
					++nrQuestion;
				} else if (currentQuestion == null) {
					//telkens we de huidige nog ni gevonden hebben wordt deze overschreven -> dit stop wanneer de huidige gevonden is
					previousQuestion = q; 	
				} else {
					//wanneer de huidige gevonden is, komt het de volgende keer in deze loop als er nog questions zijn
					nextQuestion = q;
					break; //hierna gaan we uit de loop zodat we zeker de nextQuestion niet overschrijven
				}
			}
			
			if (previousQuestion != null) {
				addHeaderLink(request, 1, "< Vorige", "previous");
				request.setAttribute("previousQuestionId", previousQuestion.getId());
			}
			
			if (nextQuestion != null) {
				addHeaderLink(request, 2, "Volgende >", "next");
				request.setAttribute("nextQuestionId", nextQuestion.getId());
			}
			
			int process = nrQuestion * 100 / survey.getQuestionSet().size();
			request.setAttribute("process", process);
			request.setAttribute("question", currentQuestion);
			setEnqueteFormAttributes(request, response);
		} catch (NumberFormatException nfe) {
			return 404;
		}
		return 200;
	}

	@Override
	protected int handlePost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("POST");
		String questionIdString;
		if(request.getParameter("previous") != null) {
			questionIdString = request.getParameter("previousQuestionId");
		} else {
			questionIdString = request.getParameter("nextQuestionId");
		}
		
		try {
			int questionId = Integer.parseInt(questionIdString);
			request.setAttribute("id", questionIdString);
			return handleGet(request, response);
		} catch (NumberFormatException nfe) {
			logger.error(nfe.toString());
			return 404;
		}
	}

	private void setEnqueteFormAttributes(HttpServletRequest request, HttpServletResponse response) {
		setForm(request, true);
		setFormActionUrl(request, response.encodeURL(request.getContextPath() + "/user/enquete/question"));
		setPage(request, "question");
		setTitle(request, "Vraag: ");
		setHeader(request, "questionHeader");
		setNavigationSelectedLink(request, 1);
	}

}
